package com.wyw.learning.offer;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * 输入一个列表，倒序返回每个节点
 *
 * @Author Mr Wu yewen.wu.china@gmail.com
 * @Date 2023/2/15 10:02
 */
public class Offer006 {

    public static void main(String[] args) {

        System.out.println(test1());

    }

    public static int[] test() {
        List<Integer> list = new ArrayList();
        Solution solution = new Solution();
        Solution.Node node = solution.initData();
        while (node != null) {
            list.add(node.val);
            node = node.next;
        }
        int l = list.size();
        int[] result = new int[l];
        int j=0;
        for(int i = l-1; i>=0; i--) {
            result[j] = list.get(i);
            j++;
        }
        return result;



    }

    static Stack<Integer> stack = new Stack();

    public static int[] test1() {
        Solution solution = new Solution();
        Solution.Node node = solution.initData();
        int len = 0;
        while (node != null) {
            stack.add(node.val);
            node = node.next;
            len ++;
        }
        int[] result = new int[len];
        int i = 0;
        while (!stack.isEmpty()) {
            result[i] = stack.pop();
            i++;
        }
        return result;

    }

    public static int[] test2(Solution.Node head) {
        Solution.Node node = head;
        int count = 0;
        while (node != null) {
            ++count;
            node = node.next;
        }
        int[] nums = new int[count];
        node = head;
        for (int i = count - 1; i >= 0; --i) {
            nums[i] = node.val;
            node = node.next;
        }
        return nums;
    }


}

class Solution{

    public Solution.Node initData() {
        Node node = new Node(1, null);
        Node node1 = new Node(2, node);
        Node node2 = new Node(3, node1);
        Node node3 = new Node(4, node2);
        Node node4 = new Node(5, node3);
        return node4;
    }

    public class Node {
        int val;
        Solution.Node next;

        public Node(int val, Solution.Node next) {
            this.val = val;
            this.next = next;
        }
    }

}
